import { OnInit, Component, Output, Input, EventEmitter } from '@angular/core';
import { AcademyService } from '../../project/service/icm/icm-base/academy/academy.service';
import { AcademyDTO } from '../../project/model/icm/icm-base/academy/academy.dto';

@Component({
  selector: 'app-academy-select',
  templateUrl: './academy-select.component.html',
  styles: [],
})
export class AcademySelectComponent implements OnInit {
  constructor(private academyService: AcademyService) {}

  @Input()
  academyId: string;
  @Output()
  academyIdChange = new EventEmitter<string>();

  /**
   * 学院名称
   */
  @Input()
  academyName: string;

  /**
   * 是否预览
   */
  @Input()
  isPreView: boolean;

  /**
   * 学院下拉
   */
  academyList: Array<AcademyDTO> = [];

  /**
   * 学院下拉加载
   */
  isLoading = false;

  /**
   * 获取学院下拉
   */
  getAcademyDropDownList() {
    this.academyService.findAllBySort('showOrder', 'asc').subscribe(data => {
      this.isLoading = false;
      this.academyList.forEach(academy => {
        // 删除与原有学院重复的学院
        data.splice(
          data.findIndex(item => item === academy),
          1,
        );
      });
      this.academyList = [...this.academyList, ...data];
    });
  }

  /**
   * 弹射数据
   */
  ngModelChange($event: string) {
    this.academyIdChange.emit($event);
  }

  ngOnInit() {
    this.getAcademyDropDownList();
  }
}
